#pragma once

#include <memory>
#include <string>

#include "SocketDefine.h"
#include "SocketAddress.hpp"

namespace nio {

	class Socket
	{
	private:
		SOCKET fd_ = INVALID_FD;
	public:
		Socket(SocketFamily family, SocketType type, int protocol = 0);

		Socket(SOCKET socket);
		virtual ~Socket();

		void Bind(const char* ip, int port);
		void Listen(int backlog = 128);
		Socket* Accept();
		void Connect(sockaddr* addr, int len);
		void Connect(const char* ipv4, int port);
		int Recv(char* buf, int len, int flags = 0);
		int Send(char* buf, int len, int flags = 0);
		int Recvfrom(char* buf, int len, sockaddr* from, int* fromlen, int flags = 0);
		int Sendto(char* buf, int len, sockaddr* to, int tolen, int flags = 0);
		void Close();
		void ReuseAddr(bool enable);
		void SetNonblocking(bool enable);

	public:
		SOCKET getfd() { return fd_; }
		SocketAddress getLocalAddress() { return SocketAddress(fd_, true); }
		SocketAddress getRemoteAddress() { return SocketAddress(fd_, false); }
	};

}